<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref, unref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { useI18n } from '@/hooks/web/useI18n'
import {
  ElMessage,
  ElIcon
} from 'element-plus'

const PATH_URL = import.meta.env.VITE_API_BASE_PATH

const { t } = useI18n()

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})

const imageUrl = ref('')

const formSchema = ref<FormSchema[]>([
  {
    field: 'type',
    label: t('sales.banner.type'),
    component: 'Select',
    componentProps: {
      options: [
        {
          label: t('sales.banner.homeBanner'),
          value: 1
        },
        {
          label: t('sales.banner.categoryBanner'),
          value: 2
        }
      ]
    }
  },
  {
    field: 'hrefUrl',
    label: t('sales.banner.hrefUrl'),
    component: 'Input'
  },
  {
    field: 'imgUrl',
    label: t('sales.banner.imgUrl'),
    component: 'Upload',
    componentProps: {
      action: PATH_URL + '/upload/image',
      showFileList: false,
      onSuccess: (_response, uploadFile) => {
        imageUrl.value = _response.url || URL.createObjectURL(uploadFile.raw!)
      },
      beforeUpload: (rawFile) => {
        if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
          ElMessage.error('Banner picture must be JPG,PNG format!')
          return false
        } else if (rawFile.size / 1024 / 1024 > 2) {
          ElMessage.error('Banner picture size can not exceed 2MB!')
          return false
        }
        return true
      },
      slots: {
        default: () => (
          <>
            {imageUrl.value ? <img src={imageUrl.value} /> : null}
            {!imageUrl.value ? (
              <ElIcon class="image-uploader-icon">
                <Icon icon="vi-ep:upload"/>
              </ElIcon>
            ) : null}
          </>
        )
      }
    }
  },
])

const rules = reactive({
  imgUrl: [required()],
  type: [required()]
})

const { formRegister, formMethods } = useForm()
const { setValues, getFormData, getElFormExpose } = formMethods

const submit = async () => {
  const elForm = await getElFormExpose()
  const valid = await elForm?.validate().catch((err) => {
    console.log(err)
  })
  if (valid) {
    const formData = await getFormData()
    formData.imgUrl = unref(imageUrl)
    return formData
  }
}

watch(
  () => props.currentRow,
  (currentRow) => {
    if (!currentRow) return
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="formSchema" />
</template>

<style lang="less">
.cell {
  height: 30px;
  padding: 3px 0;
  box-sizing: border-box;

  .text {
    position: absolute;
    left: 50%;
    display: block;
    width: 24px;
    height: 24px;
    margin: 0 auto;
    line-height: 24px;
    border-radius: 50%;
    transform: translateX(-50%);
  }

  &.current {
    .text {
      color: #fff;
      background: #626aef;
    }
  }

  .holiday {
    position: absolute;
    bottom: 0;
    left: 50%;
    width: 6px;
    height: 6px;
    background: var(--el-color-danger);
    border-radius: 50%;
    transform: translateX(-50%);
  }
}

.transfer-footer {
  padding: 6px 5px;
  margin-left: 15px;
}

.el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  transition: var(--el-transition-duration-fast);
}

.el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.image-uploader-icon {
  width: 496px;
  height: 200px;
  font-size: 28px;
  color: #8c939d;
  text-align: center;
}
</style>